package com.youlu.campus.web.order.controller;

import com.youlu.campus.entity.VO.req.DigitalCaPlaceOrder;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.order.service.DigitalCaOrderService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 电子证书支付订单
 */
@RestController
public class DigitalCaOrderController {

    @Autowired
    private DigitalCaOrderService digitalCaOrderService;

    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;

    @PostMapping("/mini-app/digital-ca/place-order")
    public QResult placeOrder(@RequestBody @Validated DigitalCaPlaceOrder digitalCaPlaceOrder,
                              BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        digitalCaPlaceOrder.setClientIp(request.getRemoteAddr());
        digitalCaPlaceOrder.setUrl(notifyUrl);
        digitalCaPlaceOrder.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(digitalCaOrderService.placeOrder(digitalCaPlaceOrder));
    }

    @PostMapping("/mini-app/digital-ca/detail")
    public QResult detail(@RequestParam String orderNo){
     return QResult.success(digitalCaOrderService.detail(orderNo));
    }


}
